package com.asset.dispoal.role.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.role.domain.db.entity.GbRoleDo;
import com.asset.dispoal.role.domain.db.mapper.GbRoleMapper;
import com.asset.dispoal.role.dto.GbRoleRequest;
import com.asset.dispoal.role.dto.GbRoleResponse;
import com.asset.dispoal.role.service.converter.RoleConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/2$
 */
@Component
public class QueryRoleCommand implements BaseCommand<GbRoleRequest, Result> {

    @Autowired
    GbRoleMapper roleMapper;

    @Override
    public Result<PageVO<GbRoleResponse>> execute(GbRoleRequest request) {
        final GbRoleDo gbRoleDo = RoleConverter.INSTANCE.reqToDo(request);
        final int count = roleMapper.count(gbRoleDo);
        final Page<GbRoleDo> page = PagingQuery.getPage(count, gbRoleDo);
        PageVO<GbRoleResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            final List<GbRoleDo> doList = roleMapper.page(page);
            final List<GbRoleResponse> gbRoleResponses = RoleConverter.INSTANCE.dosToRes(doList);
            pageVO.setResult(gbRoleResponses);
        }
        return Result.ok(pageVO);
    }
}
